<?php
/**
 * 分享id标识符
 */
namespace Control;
use Model\ShareIdMod;
use Pub\cache\Cache;
use Pub\tool\Tool;

defined('IN_IA') or exit('Access Denied');
class ShareIdControl extends Control
{
    private $ShareId = null;
    public function __construct()
    {
        parent::__construct();
        $this->ShareId = new ShareIdMod();
    }

    /**
     * 通过一个会员id和一个红包id获取一个分享码
     * @param $_uid
     * @param $_red_id
     * @return string id 返回分享标识
     */
    public function findId($_uid,$_red_id)
    {
        $_data = Cache::get(Cache::SHARE_ID,array("uid"=>$_uid,"red_id"=>$_red_id));
        if(!$_data || KUYUAN_DEBUG){
            if($_data = $this->ShareId->findOne(array("uid"=>$_uid,"red_id"=>$_red_id),array("id"))){
                $_data = $_data["id"];
            }else{
                $_data = array("uid"=>$_uid,"red_id"=>$_red_id,"id"=>Tool::createCode());
                if($this->ShareId->insert($_data)){
                    $_data = $_data["id"];
                }
            }
            Cache::set(Cache::SHARE_ID,$_data,array("uid"=>$_uid,"red_id"=>$_red_id));
        }
        return $_data;
    }

    /**
     * 通过分享id查询uid和red_id
     * @param string $_id 分享标识id
     * @return Array 返回uid和red_id
     */
    public function find($_id)
    {
        $_data = Cache::get(Cache::SHARE_UID_REDID,array("share_id"=>$_id));
        if(!$_data || KUYUAN_DEBUG){
            if($_data = $this->ShareId->findOne(array("id"=>$_id),array("uid","red_id"))){
                Cache::set(Cache::SHARE_UID_REDID,$_data,array("share_id"=>$_id));
            }
        }
        return $_data;
    }
}